package com.example.service;

import com.example.util.EncryptUtils;
import com.mchange.v1.lang.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;


@Service
public class MailService {

    @Value("${smtp.server:''}")
    private String server;

    @Value("${smtp.account:''}")
    private String account;

    @Value("${smtp.port:''}")
    private String port;

    @Value("${smtp.send:''}")
    private String send;

    @Value("${smtp.password:''}")
    private String password;

    @Value("${smtp.ssl:''}")
    private String ssl;

    @Value("${smtp.tls:''}")
    private String tls;

    private static Logger logger = LoggerFactory.getLogger(ZhiHuService.class);

    private JavaMailSenderImpl getMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setHost(server);
        javaMailSender.setPort(Integer.valueOf(port));
        Properties props = new Properties();
        props.put("mail.smtp.auth", String.valueOf(false));
        if (!StringUtils.isEmpty(password)) {
            javaMailSender.setUsername(account);
            javaMailSender.setPassword(EncryptUtils.aesDecrypt(password).toString());
            props.put("mail.smtp.auth", String.valueOf(true));
        }
        if (BooleanUtils.parseBoolean(ssl)) {
            String stlKey = "mail.smtp.socketFactory.class";
            String stlValue = "javax.net.ssl.SSLSocketFactory";
            props.put(stlKey, stlValue);
        }
        if (BooleanUtils.parseBoolean(tls)) {
            String ssl = "mail.smtp.starttls.enable";
            props.put(ssl, String.valueOf(true));
        }
        javaMailSender.setJavaMailProperties(props);
        return javaMailSender;
    }

    @Async
    public void sendSimpleEmail(String subject, String content, String... to) {
        try {
            JavaMailSenderImpl javaMailSender = getMailSender();
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setTo(to);
            simpleMailMessage.setFrom(javaMailSender.getUsername());
            simpleMailMessage.setSentDate(new Date());
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            javaMailSender.send(simpleMailMessage);
        } catch (Exception exception) {
            logger.info(exception.getMessage());
            logger.error(exception.getMessage());
        }
    }

    @Async
    public void sendHtmlEmail(String subject, String content, String... to) {
        JavaMailSenderImpl javaMailSender = getMailSender();
        MimeMessage message = javaMailSender.createMimeMessage();//创建一个MINE消息
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(send);
            helper.setTo(to);
            helper.setSentDate(new Date());
            helper.setSubject(subject);
            helper.setText(content, true);
            javaMailSender.send(message);
        } catch (MessagingException exception) {
            logger.info(exception.getMessage());
            logger.error(exception.getMessage());
        }
    }

}
